package de.syfy.project.engine.hud;

import de.syfy.project.engine.interfaces.Game;
import java.awt.geom.Point2D;
import de.syfy.project.engine.hud.interfaces.UI_Item;
import de.syfy.project.engine.hud.interfaces.UI_Container;
import de.syfy.project.engine.TextureLoader;
import java.io.IOException;
import java.util.ArrayList;
import de.syfy.project.engine.Texture;
import java.awt.Dimension;
import java.util.List;
import static org.lwjgl.opengl.GL11.*;

/**
 *
 * @author Timo
 */
public class Panel implements UI_Container{

    private Game game;
    private Texture bg_texture;
    private Dimension size;
    private Point2D.Float p;
    private int priority;
    private List<UI_Item> items;
    
    public Panel(Game game, TextureLoader loader, String texturePath,Dimension size, Point2D.Float p, int priority){
        try {
            this.game = game;
            bg_texture = loader.getTexture(texturePath);
            this.size = size;
            this.p = p;
            this.priority = priority;
            this.items = new ArrayList<UI_Item>();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    @Override
    public void setSize(Dimension d) {
        this.size = d;
        bg_texture.setHeight(d.height);
        bg_texture.setWidth(d.width);
    }

    @Override
    public Dimension getSize() {
        return size;
    }
    
    @Override
    public void setPosition(Point2D.Float p){
        this.p = p;
    }
    
    @Override
    public Point2D.Float getPosition(){
        return p;
    }
    
    
    @Override
    public void setPriority(int priority) {
        this.priority = priority;
    }

    @Override
    public int getPriority() {
        return priority;
    }

    @Override
    public void setBackground(Texture texture) {
        this.bg_texture = texture;
        if(size != null){
            bg_texture.setHeight(size.height);
            bg_texture.setWidth(size.width);
        }
    }

    @Override
    public void addItem(UI_Item item) {
        if(items != null && item != null){
            items.add(item);
        }else{
            items = new ArrayList<UI_Item>();
            items.add(item);
        }
    }

    @Override
    public void removeItem(UI_Item item) {
        if(items != null && item != null){
            items.remove(item);
        }
    }

    @Override
    public void drawContainer() {
        glEnable(GL_TEXTURE_2D); //Um zu gewährleisten dass Schriften später funzen
        glPushMatrix();
        bg_texture.bind();
        glTranslatef(p.x, p.y, 0);
        glTranslatef(0,0,0.0f);
        glBegin(GL_QUADS);
            glTexCoord2f(0, 0);
            glVertex2f(0, 0);
            glTexCoord2f(0, bg_texture.getHeight());
            glVertex2f(0, bg_texture.getImageHeight());
            glTexCoord2f(bg_texture.getWidth(), bg_texture.getHeight());
            glVertex2f(bg_texture.getImageWidth(), bg_texture.getImageHeight());
            glTexCoord2f(bg_texture.getWidth(), 0);
            glVertex2f(bg_texture.getImageWidth(), 0);
        glEnd();
        glPopMatrix();
        glDisable(GL_TEXTURE_2D); //Um zu gewährleisten dass Schriften später funzen
        for(UI_Item item: items){
            item.drawItem();
        }
    }

    /*
     * Vergleiche sind verkehrt herum, da wir den Container mit der höchsten 
     * Priorittät am Anfang haben wollen.
     */
    @Override
    public int compareTo(UI_Container container) {
        if(priority > container.getPriority()){
            return -1;
        }else if(priority < container.getPriority()){
            return 1;
        }else{
            return 0;
        }
    }

    @Override
    public void addEventListener(EventListener e) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
